package com.sc.softmgr.business.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acc.softmgr.model.SysDict;
import com.acc.softmgr.model.SysDictExample;
import com.alibaba.fastjson.JSONObject;
import com.sc.softmgr.business.service.ISysDictService;
import com.sc.softmgr.common.constant.enums.RestStatus;
import com.sc.softmgr.common.web.DataGridObject;

/**
 * 
 * @content
 * 
 * @date 2017年6月14日
 * 
 * @author dobeedai
 * 
 */
@Controller
@RequestMapping("sysDict")
public class SysDictController {

	@Autowired
	private ISysDictService sysDictService;

	@RequestMapping(value = "toSysDict", method = RequestMethod.GET)
	public String toSysDict() {
		return "sysDict/sysDict";
	}

	/**
	 * 查询列表页
	 * 
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSysDict", method = RequestMethod.POST)
	@ResponseBody
	public DataGridObject<SysDict> searchSysDict(@RequestBody SysDict sysDict, Integer page, Integer rows)
			throws Exception {
		DataGridObject<SysDict> responseObject = new DataGridObject<SysDict>();
		SysDictExample example = new SysDictExample();
		String psKey = sysDict.getPs_key();
		if (!StringUtils.isEmpty(psKey)) {
			example.getCriteria().andPs_keyEqualTo(psKey);
		}
		if (page == null || page == 0) {
			page = 1;
		}
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);

		// 如果sysDict有查询值,则需要进行查询

		List<SysDict> sysDicts = sysDictService.selectByExample(example);
		responseObject.setTotal(page * rows);
		responseObject.setRows(sysDicts);
		return responseObject;
	}

	/**
	 * 新增项目
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addSysDict", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject addSysDict(@RequestBody SysDict sysDict) throws Exception {
		JSONObject json = new JSONObject();
		String psKey = sysDict.getPs_key();
		String psValue = sysDict.getPs_value();
		if (StringUtils.isEmpty(psKey) || StringUtils.isEmpty(psValue)) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}

		SysDictExample example = new SysDictExample();
		example.getCriteria().andPs_keyEqualTo(psKey);
		example.getCriteria().andPs_valueEqualTo(psValue);
		example.getCriteria().andPs_moudleEqualTo(sysDict.getPs_moudle());
		example.getCriteria().andPs_statusEqualTo(sysDict.getPs_status());
		List<SysDict> sysDicts = sysDictService.selectByExample(example);
		if (sysDicts != null && sysDicts.size() > 0) {
			json.put("status", RestStatus.NOT_UNIQUE.code);
			json.put("message", RestStatus.NOT_UNIQUE.message);
			return json;
		}

		sysDictService.insert(sysDict);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		return json;
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSysDict", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject editSysDict(@RequestBody SysDict sysDict) throws Exception {
		JSONObject json = new JSONObject();

		if (StringUtils.isEmpty(sysDict.getPs_key()) || StringUtils.isEmpty(sysDict.getPs_value())
				|| sysDict.getId() == null || sysDict.getId() == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}

		String psKey = sysDict.getPs_key();
		String psValue = sysDict.getPs_value();
		int id = sysDict.getId();

		SysDict oldDictInfo = sysDictService.selectByPrimaryKey(id);
		if (oldDictInfo != null) {
			oldDictInfo.setPs_key(psKey);
			oldDictInfo.setPs_value(psValue);
			oldDictInfo.setPs_moudle(sysDict.getPs_moudle());
			oldDictInfo.setPs_status(sysDict.getPs_status());

			sysDictService.updateByPrimaryKey(oldDictInfo);
			json.put("status", RestStatus.SUCCESS.code);
			json.put("message", RestStatus.SUCCESS.message);
			return json;
		}

		json.put("status", RestStatus.EXIT_USER.code);
		json.put("message", RestStatus.EXIT_USER.message);
		return json;
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchSysDict", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject searchSysDict(@PathVariable java.lang.Integer id) throws Exception {
		JSONObject json = new JSONObject();
		if (id == null) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SysDict sysDict = sysDictService.selectByPrimaryKey(id);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		json.put("data", sysDict);
		return json;
	}

	/**
	 * 删除项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSysDict", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject removeSysDict(@Validated java.lang.Integer id) throws Exception {
		JSONObject json = new JSONObject();
		if (id == null) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		// SysDict sysDict = sysDictService.selectByPrimaryKey(id);
		sysDictService.deleteByPrimaryKey(id);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		return json;
	}

}
